//
//  ViewController.m
//  YJ
//
//  Created by 吴志勇 on 2020/4/2.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "ViewController.h"
#import "HomeViewController.h"
#import "LoginApi.h"
#import <CommonCrypto/CommonDigest.h>

#define kToken @"token"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//
- (void)isTonke{
    if ([[NSUserDefaults standardUserDefaults] stringForKey:kToken].length > 0) {
        [self presentVC];
    }
}


- (void)presentVC{
   
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: [[HomeViewController alloc] init]];
    self.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
    
};

- (IBAction)loginClick:(id)sender {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:@"15715655233" forKey:@"username"];
    [dict setValue:[self md5:@"123456"] forKey:@"password"];
    LoginApi *api = [[LoginApi alloc] initWithUrl:@"/dataApi/appUser/login" requestArgument:dict];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKRequest * _Nullable request, NSDictionary * _Nullable result, BOOL success) {
        NSDictionary *dataDict = result[@"data"];
        NSString *token = dataDict[@"token"];
        if (token.length>1) {
            [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken];
            [self presentVC];
        }
    } failure:^(__kindof YTKRequest * _Nullable request, NSString * _Nullable errorInfo) {
//        NSLog(@"fail");
    }];
}

- (IBAction)registClik:(id)sender {
}


- (NSString *) md5 : (NSString *) str {
 
    if (! str) return nil;
    
    const char *cStr = str.UTF8String;
    
    unsigned char result[CC_MD5_DIGEST_LENGTH];
   
    CC_MD5(cStr, (CC_LONG) strlen(cStr), result);
    NSMutableString *md5Str = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        [md5Str appendFormat:@"%02x", result[i]];
    }
    return md5Str;
}

@end
